<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8"
          ><el-input placeholder="请输入内容" v-model="selectMsg.msg">
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="handSelect"
            ></el-button> </el-input
        ></el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogvisible = true"
            >添加用户</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="userlist" stripe border>
        <el-table-column prop="id" label="id" width="50px"> </el-table-column>
        <el-table-column prop="username" label="用户名"> </el-table-column>
        <el-table-column prop="password" label="密码"> </el-table-column>
        <el-table-column prop="createdtime" label="创建时间"> </el-table-column>
        <el-table-column prop="updatedtime" label="更新时间"> </el-table-column>
        <el-table-column prop="remarks" label="备注"> </el-table-column>
        <el-table-column prop="mg_state" label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              @change="userStatechanged(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEidtDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeUserInfo(scope.$index, scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加用户对话框区域 -->
    <el-dialog
      title="添加用户"
      :visible.sync="addDialogvisible"
      width="50%"
      @close="addDialogclosed"
    >
      <!-- 内容主体区域 -->
      <span>
        <el-form
          :model="addForm"
          :rules="addFormRules"
          ref="addFormRef"
          label-width="70px"
        >
          <!-- 用户名 -->
          <el-form-item label="用户名" prop="username">
            <el-input v-model="addForm.username"></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item label="密码" prop="password">
            <el-input v-model="addForm.password"></el-input>
          </el-form-item>
          <!-- 备注 -->
          <el-form-item label="备注" prop="remarks">
            <el-input v-model="addForm.remarks"></el-input>
          </el-form-item>
        </el-form>
      </span>
      <!-- 底部按钮区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogvisible = false">取 消</el-button>
        <el-button type="primary" @click="addDialogvisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  mounted () {
    this.getUserList()
  },
  data () {
    return {
      // 搜索框内容
      selectMsg: {
        msg: ''
      },
      // 要操作的行
      currentIndex: 0,
      // 用户列表
      userlist: [],
      // 控制添加用户对话框的显示和隐藏，默认为false
      addDialogvisible: false,
      // 添加用户表单数据
      addForm: {
        username: '',
        password: '',
        remarks: ''
      },
      // 添加用户表单验证规则
      addFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getUserList()
  },
  methods: {
    // 根据id 用户名 备注查找用户
    handSelect () {
      if (this.selectMsg.msg) {
        this.SelectUser(this.selectMsg)
      } else {
        this.getUserList()
      }
    },
    // 删除用户
    removeUserInfo (index, id) {
      this.DelectUser(index, id)
    },
    // 监听添加用户对话框关闭的事件
    addDialogclosed () {
      this.$refs.addFormRef.resetFields()
    },
    // 获取用户列表
    async getUserList () {
      const { data: res } = await this.$http.get('user')
      this.userlist = res.data
    },
    // 查寻方法
    async SelectUser (params) {
      const { data: res } = await this.$http.get('user/select', { params: params })
      console.log(res)
      this.userlist = res.data
      this.userlist.unshift(res.data2)
    },
    // 删除方法
    async DelectUser (index, id) {
      await this.$http.delete(`user/${id}`)
      this.currentIndex = index
      this.userlist.splice(this.currentIndex, 1)
    }
  }
}
</script>

<style scoped>
</style>
